package com.jyb.util.capture;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.alibaba.fastjson.JSON;
import com.jyb.config.HttpCilentConfig;
import com.jyb.enumeration.ShanghaiDistrict;
import com.jyb.util.UrlUtil;
import com.jyb.vo.JxjhVo;

public class JxjhUtil {

	public static List<JxjhVo> capture(String cm){
		
		List<JxjhVo> list=new ArrayList<JxjhVo>();
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response=null;
		try {
			
			String url="http://www.fob001.cn/guestbook/jxjh/index.php?page=1&name="+cm;
			url=UrlUtil.hanadleUrl(url);
			
			HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
			response = httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			String str=EntityUtils.toString(entity);
			Document document=Jsoup.parse(str);
			
			Elements elements=document.select("table[bgcolor=#9999FF]").get(0).select("tr:gt(0)");
			for (Element element : elements) {
				Elements elementTds=element.select("td");
				JxjhVo jxjhVo=new JxjhVo();
				jxjhVo.setCm(elementTds.get(0).text());
				jxjhVo.setHc(elementTds.get(1).text());
				jxjhVo.setKg(elementTds.get(2).text());
				jxjhVo.setJg(elementTds.get(3).text());
				jxjhVo.setGq(elementTds.get(4).text());
				list.add(jxjhVo);
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(response!=null){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println(JSON.toJSONString(list));
		return list;
	}
	
	public static List<JxjhVo> capture2(ShanghaiDistrict district){
		
		List<JxjhVo> list=new ArrayList<JxjhVo>();
		
		CloseableHttpClient httpclient = HttpClients.createDefault();
		CloseableHttpResponse response=null;
		try {
			
			if(ShanghaiDistrict.WY==district){
				String url="http://www.spict.com/portinfo/Normal/CTNInPlanPage.aspx";
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				Elements elements=document.select(".dxpPageNumber");
				
				for(int i=0;i<elements.size();i++){
					HttpPost httpPost = new HttpPost("http://www.spict.com/portinfo/Normal/CTNInPlanPage.aspx");
					List <NameValuePair> nvps = new ArrayList <NameValuePair>();
					for (Element element : elements) {
						String name=element.attr("name");
						String val= element.val();
						if("__CALLBACKPARAM".equals(name)){
							nvps.add(new BasicNameValuePair(name,"c0:GB|20;12|PAGERONCLICK3|PN"+i+";"));
						}else{
							nvps.add(new BasicNameValuePair(name,val));
						}
						
					}
					httpPost.setEntity(new UrlEncodedFormEntity(nvps));
					CloseableHttpResponse response2 = httpclient.execute(httpPost);
					entity = response2.getEntity();
					str=EntityUtils.toString(entity);
					document=Jsoup.parse(str);
					
					Elements element2s=document.select("table[id=ctl00_ASPxSplitter2_ContentPlaceHolder1_gridView_DXMainTable]").select("tr:gt(1)");
					for (Element element : element2s) {
						Elements elementTds=element.select("td");
						
						if(elementTds.size()>=9){
							JxjhVo jxjhVo=new JxjhVo();
							jxjhVo.setGq(district.name());
							jxjhVo.setGqmc(district.getTitle());
							
							jxjhVo.setCm(elementTds.get(1).text());
							jxjhVo.setYwcm(elementTds.get(2).text());
							jxjhVo.setKg(elementTds.get(5).text());
							jxjhVo.setJg(elementTds.get(6).text());
							jxjhVo.setDl(elementTds.get(8).text());
							list.add(jxjhVo);
						}
						
					}
				}
				
			}else if(ShanghaiDistrict.WE==district){
				String url="http://www.sipgzct.com/wat/controllerServlet.do?queryid=0002001&querysid=g0002&method=doquery";
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				String p_pages=document.select(".p_pages").get(0).text();
				p_pages=p_pages.replaceAll(" ", "");
				p_pages=p_pages.substring(p_pages.indexOf("/")+1);
				Integer totalPage=Integer.parseInt(p_pages);
				
				for(int i=1;i<=totalPage;i++){
					httpGet=HttpCilentConfig.getHttpGet("http://www.sipgzct.com/wat/controllerServlet.do?queryid=0002001&method=doquery&showpage="+i);
					CloseableHttpResponse response2 = httpclient.execute(httpGet);
					entity = response2.getEntity();
					str=EntityUtils.toString(entity);
					document=Jsoup.parse(str);
					
					Elements element2s=document.select("table[class=ltable]").select("tr:gt(0)");
					for (Element element : element2s) {
						Elements elementTds=element.select("td");
						
						if(elementTds.size()>=10){
							JxjhVo jxjhVo=new JxjhVo();
							jxjhVo.setGq(district.name());
							jxjhVo.setGqmc(district.getTitle());
							
							jxjhVo.setCm(elementTds.get(0).text());
							jxjhVo.setYwcm(elementTds.get(1).text());
							jxjhVo.setCsi(elementTds.get(2).text());
							jxjhVo.setHc(elementTds.get(3).text());
							jxjhVo.setJck(elementTds.get(4).text());
							jxjhVo.setKg(elementTds.get(5).text());
							jxjhVo.setJg(elementTds.get(6).text());
							jxjhVo.setDl(elementTds.get(7).text());
							list.add(jxjhVo);
						}
						
					}
				}
			}else if(ShanghaiDistrict.WS==district){
				String url="http://www.sect.com.cn/wat/controllerServlet.do?queryid=0002001&querysid=g0002&method=doquery";
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				String p_pages=document.select(".p_pages").get(0).text();
				p_pages=p_pages.replaceAll(" ", "");
				p_pages=p_pages.substring(p_pages.indexOf("/")+1);
				Integer totalPage=Integer.parseInt(p_pages);
				
				for(int i=1;i<=totalPage;i++){
					httpGet=HttpCilentConfig.getHttpGet("http://www.sect.com.cn/wat/controllerServlet.do?queryid=0002001&method=doquery&showpage="+i);
					CloseableHttpResponse response2 = httpclient.execute(httpGet);
					entity = response2.getEntity();
					str=EntityUtils.toString(entity);
					document=Jsoup.parse(str);
					
					Elements element2s=document.select("table[class=ltable]").select("tr:gt(0)");
					for (Element element : element2s) {
						Elements elementTds=element.select("td");
						
						if(elementTds.size()>=9){
							JxjhVo jxjhVo=new JxjhVo();
							jxjhVo.setGq(district.name());
							jxjhVo.setGqmc(district.getTitle());
							
							jxjhVo.setCm(elementTds.get(0).text());
							jxjhVo.setYwcm(elementTds.get(1).text());
							jxjhVo.setCsi(elementTds.get(2).text());
							jxjhVo.setHc(elementTds.get(3).text());
							jxjhVo.setJck(elementTds.get(4).text());
							jxjhVo.setKg(elementTds.get(5).text());
							jxjhVo.setJg(elementTds.get(6).text());
							jxjhVo.setDl(elementTds.get(7).text());
							list.add(jxjhVo);
						}
						
					}
				}
			}else if(ShanghaiDistrict.WW==district){
				String url="https://www.smct.com.cn/wat/controllerServlet.do?queryid=0002001&querysid=g0002&method=doquery";
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				String p_pages=document.select(".p_pages").get(0).text();
				p_pages=p_pages.replaceAll(" ", "");
				p_pages=p_pages.substring(p_pages.indexOf("/")+1);
				Integer totalPage=Integer.parseInt(p_pages);
				
				for(int i=1;i<=totalPage;i++){
					httpGet=HttpCilentConfig.getHttpGet("http://www.smct.com.cn/wat/controllerServlet.do?queryid=0002001&method=doquery&showpage="+i);
					CloseableHttpResponse response2 = httpclient.execute(httpGet);
					entity = response2.getEntity();
					str=EntityUtils.toString(entity);
					document=Jsoup.parse(str);
					
					Elements element2s=document.select("table[class=ltable]").select("tr:gt(0)");
					for (Element element : element2s) {
						Elements elementTds=element.select("td");
						
						if(elementTds.size()>=8){
							JxjhVo jxjhVo=new JxjhVo();
							jxjhVo.setGq(district.name());
							jxjhVo.setGqmc(district.getTitle());
							
							jxjhVo.setCm(elementTds.get(0).text());
							jxjhVo.setYwcm(elementTds.get(1).text());
							jxjhVo.setHc(elementTds.get(2).text());
							jxjhVo.setJck(elementTds.get(3).text());
							jxjhVo.setKg(elementTds.get(4).text());
							jxjhVo.setJg(elementTds.get(5).text());
							jxjhVo.setDl(elementTds.get(6).text());
							list.add(jxjhVo);
						}
						
					}
				}
			}else if(ShanghaiDistrict.YY==district){
				String url="http://cx.shsict.com:8100/wat/controllerServlet.do?queryid=0002001&querysid=g0002&method=doquery";
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				String p_pages=document.select(".p_pages").get(0).text();
				p_pages=p_pages.replaceAll(" ", "");
				p_pages=p_pages.substring(p_pages.indexOf("/")+1);
				Integer totalPage=Integer.parseInt(p_pages);
				
				for(int i=1;i<=totalPage;i++){
					httpGet=HttpCilentConfig.getHttpGet("http://cx.shsict.com:8100/wat/controllerServlet.do?queryid=0002001&method=doquery&showpage="+i);
					CloseableHttpResponse response2 = httpclient.execute(httpGet);
					entity = response2.getEntity();
					str=EntityUtils.toString(entity);
					document=Jsoup.parse(str);
					
					Elements element2s=document.select("table[class=ltable]").select("tr:gt(0)");
					for (Element element : element2s) {
						Elements elementTds=element.select("td");
						
						if(elementTds.size()>=9){
							JxjhVo jxjhVo=new JxjhVo();
							jxjhVo.setGq(district.name());
							jxjhVo.setGqmc(district.getTitle());
							
							jxjhVo.setCm(elementTds.get(0).text());
							jxjhVo.setYwcm(elementTds.get(1).text());
							jxjhVo.setCsi(elementTds.get(2).text());
							jxjhVo.setHc(elementTds.get(3).text());
							jxjhVo.setJck(elementTds.get(3).text());
							jxjhVo.setJck(elementTds.get(4).text());
							jxjhVo.setKg(elementTds.get(5).text());
							jxjhVo.setJg(elementTds.get(6).text());
							jxjhVo.setDl(elementTds.get(7).text());
							list.add(jxjhVo);
						}
						
					}
				}
			}else if(ShanghaiDistrict.YS==district){
				String url="http://www.sgict.com.cn:7001/query/search/comm/continplan.jsp";
				HttpGet httpGet = HttpCilentConfig.getHttpGet(url);
				response = httpclient.execute(httpGet);
				HttpEntity entity = response.getEntity();
				String str=EntityUtils.toString(entity);
				Document document=Jsoup.parse(str);
				
				String p_pages=document.text();
				p_pages=p_pages.substring(p_pages.indexOf("总共有")+3,p_pages.indexOf("页("));
				Integer totalPage=Integer.parseInt(p_pages);
				
				for(int i=1;i<=totalPage;i++){
					httpGet=HttpCilentConfig.getHttpGet("http://www.sgict.com.cn:7001/query/search/comm/continplan.jsp?page="+i);
					CloseableHttpResponse response2 = httpclient.execute(httpGet);
					entity = response2.getEntity();
					str=EntityUtils.toString(entity);
					document=Jsoup.parse(str);
					
					if(document.select("tr[class=result_th]").size()>0){
						Elements elements=document.select("tr[class=result_th]").get(0).parent().select("tr:gt(0)");
						for (Element element : elements) {
							Elements elementTds=element.select("td");
							
							if(elementTds.size()>=8){
								JxjhVo jxjhVo=new JxjhVo();
								jxjhVo.setGq(district.name());
								jxjhVo.setGqmc(district.getTitle());
								
								jxjhVo.setCm(elementTds.get(0).text());
								jxjhVo.setYwcm(elementTds.get(1).text());
								jxjhVo.setCsi(elementTds.get(2).text());
								jxjhVo.setHc(elementTds.get(3).text());
								jxjhVo.setJck(elementTds.get(4).text());
								jxjhVo.setKg(elementTds.get(5).text());
								jxjhVo.setJg(elementTds.get(6).text());
								jxjhVo.setDl(elementTds.get(7).text());
								list.add(jxjhVo);
							}
						}
					}
				}
			}
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(response!=null){
				try {
					response.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		System.out.println(list.size());
		System.out.println(JSON.toJSONString(list));
		return list;
	}
	
	public static void main(String[] args) {
		JxjhUtil.capture2(ShanghaiDistrict.YS);
			
	}
}
